package com.webridge.plm.datautility;

import org.apache.log4j.Logger;

import wt.fc.Persistable;
import wt.fc.PersistenceHelper;
import wt.part.WTPart;
import wt.util.WTException;

import com.ptc.core.components.descriptor.ModelContext;
import com.ptc.core.components.factory.AbstractDataUtility;
import com.ptc.core.components.factory.dataUtilities.AttributeDataUtilityHelper;
import com.ptc.core.components.factory.dataUtilities.DefaultDataUtility;
import com.ptc.core.components.rendering.guicomponents.GUIComponentArray;
import com.ptc.core.components.rendering.guicomponents.GuiComponentUtil;
import com.ptc.core.components.rendering.guicomponents.TextBox;
import com.ptc.core.meta.type.common.impl.DefaultTypeInstance;
import com.ptc.core.ui.resources.ComponentMode;

/**
 * 1级分类
 * 
 * @author kira
 */
public class EnglishNameDataUtility extends AbstractDataUtility {
    private static Logger logger = Logger.getLogger(EnglishNameDataUtility.class.getName());

    @Override
    public Object getDataValue(String componentId, Object datum, ModelContext modelContext) throws WTException {

        logger.debug("english name datautility is coming ");
        ComponentMode componentModel = modelContext.getDescriptorMode();
        String actionType = componentModel.toString();
        logger.debug("actionType en:" + actionType +",componentModel:" + componentModel.toString());
        GUIComponentArray array = new GUIComponentArray();
        if (componentModel.equals(ComponentMode.CREATE)
                || componentModel.equals(ComponentMode.EDIT)) { 
	        TextBox textBox = GuiComponentUtil.createTextBox(50);
	        textBox.setId(componentId);
	        textBox.setEnabled(true);
	        textBox.setColumnName(AttributeDataUtilityHelper.getColumnName(componentId, datum, modelContext));
	        if (componentModel.equals(ComponentMode.EDIT)) {
	        	logger.debug("edit english name is coming = ");
	        	logger.debug("attribute container = "+((DefaultTypeInstance)datum).getAttributeContainer().get(modelContext.getATI()));
	        	if (((DefaultTypeInstance)datum).getAttributeContainer().get(modelContext.getATI()) != null) {	        		
	        		String value = ((DefaultTypeInstance)datum).getAttributeContainer().get(modelContext.getATI()).toString();
	        		textBox.setValue(value);        		
	        	}
	        }
//          textBox.setValue("EN");//设置值
	        textBox.addJsAction("onkeyup", "AutoComplete_en()");
	        textBox.setRequired(true);
	        //textBox.addJsAction("onblur", "validateENName()");//validate EN Names
	        array.addGUIComponent(textBox);
	        logger.debug("array = "+array);
	        return array;
        }
        return null;    
    }
}
